﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.DLR;

namespace Workflows.Components.Rules
{

    /// <summary>
    /// 
    /// </summary>
    public partial class RuleConditionEditor : Form
    {
        public event RuleDataUpdated OnRuleConditionUpdated;
        private object _OwnerRuleNode;
        private Rule _Rule;
        private DeclarativeRuleCondition _Condition;

        /// <summary>
        /// 
        /// </summary>
        public RuleConditionEditor()
        {
            InitializeComponent();
        }

        private void RuleConditionEditor_Load(object sender, EventArgs e)
        {
            this.PopulateEvaluatorOperators();
        }

        private void PopulateEvaluatorOperators()
        {
            this.cbo_Op.Items.Clear();
            for(int i=0;i<=6;i++)
            {
                EvaluationOperator op = (EvaluationOperator) i;
                this.cbo_Op.Items.Add(op.ToString());
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ownerRuleNode"></param>
        /// <param name="rule"></param>
        /// <param name="condition"></param>
        public void InitRuleCondition(object ownerRuleNode, Rule rule, DeclarativeRuleCondition condition)
        {
            this._OwnerRuleNode = ownerRuleNode;
            this._Rule = rule;
            this._Condition = condition;
            this.DisplaySettings();
        }

        private void DisplaySettings()
        {
            this.cbo_Field.Items.Clear();
            if(this._Rule !=null && (!string.IsNullOrEmpty(this._Rule.AssemblyFilePath)) && (!string.IsNullOrEmpty(this._Rule.BusinessObjectName)))
            {
                Assembly assem = Assembly.LoadFile(this._Rule.AssemblyFilePath);
                Type type = assem.GetType(this._Rule.BusinessObjectName);
                FieldInfo[] fields = type.GetFields();
                if(fields !=null && fields.Length>0)
                {
                    foreach(FieldInfo field in fields)
                    {
                        this.cbo_Field.Items.Add(field.Name);
                    }
                }
                PropertyInfo[] props = type.GetProperties();
                if(props !=null && props.Length>0)
                {
                    foreach(PropertyInfo prop in props)
                    {
                        this.cbo_Field.Items.Add(prop.Name);
                    }
                }
            }
        }

        private bool SaveSettings()
        {
            if (this.cbo_Field.SelectedIndex >= 0 && this.cbo_Op.SelectedIndex >= 0 &&
                (!string.IsNullOrEmpty(this.txt_EvaluateAgainst.Text)))
            {
                Assembly assem = Assembly.LoadFile(this._Rule.AssemblyFilePath);
                Type type = assem.GetType(this._Rule.BusinessObjectName);
                string fieldName = this.cbo_Field.SelectedItem.ToString();
                FieldInfo field = type.GetField(fieldName);
                SimpleDataType dataType = SimpleDataType.IntNumberType;
                if (field != null)
                {
                    dataType = SimpleDataTypeUtil.ToSimpleDataType(field.FieldType);
                }
                EvaluationOperator op = (EvaluationOperator) this.cbo_Op.SelectedIndex;
                string compareAgainst = this.txt_EvaluateAgainst.Text;

                this._Condition.CompareAgainst = compareAgainst;
                this._Condition.CompareOperator = op;
                this._Condition.FieldDataType = dataType;
                this._Condition.FieldName = fieldName;

                return true;
            }
            else
            {
                MessageBox.Show("not initialized");
                return false;
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            if(this.SaveSettings())
            {
                if(this.OnRuleConditionUpdated!=null)
                {
                    this.OnRuleConditionUpdated(this._OwnerRuleNode, this._Rule, this._Condition);
                }

                this.Close();
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
